﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QualityRpt.Dao;
using System.Data;
using log4net;
using QualityRpt.Utils;
using System.Threading;
using System.Threading.Tasks;

namespace QualityRpt.Service
{
    public class TransNoteService : BaseService
    {
        static ILog logger = LogManager.GetLogger("TransNoteService");
        private static ITransNoteDao _impDao = null;
        static TransNoteService()
        {
            _impDao = CreateDaoInstance("ITransNoteDao") as ITransNoteDao;
        }
        static int AccDays = 0;
        private static int GetAccDays()
        {
            if (AccDays == 0)
            {
                string straccdays = AppUtils.GetAppKey("AccDays");
                if (straccdays != string.Empty)
                {
                    AccDays = Convert.ToInt32(AccDays);
                }
                if (AccDays < 1)
                {
                    AccDays = 1;
                }
            }
            return AccDays;
        }
        /// <summary>
        /// 抽取数据 第二版 改为2天一组进行抽取
        /// 1. 四张表到Trans_Note;
        /// 2. Trans_Note 到 低电以及防拆
        /// 3. 从Trans_Note中筛选漏读
        /// 
        /// 数据处理逻辑
        /// a. 按天进行数据处理并核对
        /// 0 成功
        /// -1 抽取到交易表出错
        /// -2 抽取到筛选表出错
        /// </summary>
        /// <param name="row_count">插入到整合表的行数</param>
        public static short ExtractDataV2(DateTime startTime, DateTime endTime)
        {
            short rt = 0;
            DateTime extractDate = startTime.Date;//2014-04-12 00:00:00
            while (extractDate < endTime.Date)
            {
                DateTime extractEndDate = extractDate.AddDays(GetAccDays());//2014-04-13 00:00:00
                if (extractEndDate > endTime.Date)
                {
                    extractEndDate = endTime.Date;
                }
                string msg = string.Empty; DateTime trackStart = DateTime.Now;
                #region 从源数据抽取到交易表
                try
                {
                    trackStart = DateTime.Now;
                    _impDao.ExtractData(extractDate, extractEndDate, out msg);
                    TimeSpan ts = DateTime.Now - trackStart;
                    string logMsg = "抽取交易数据,时间段:(" + extractDate.ToShortDateString() + "到" + extractEndDate.ToShortDateString() + ");耗时:" + StringUtils.FormatHMS((long)ts.TotalSeconds) + ";" + msg;
                    logger.Info(logMsg);
                }
                catch (Exception ex)
                {
                    logger.Error(ex); 
                }
                #endregion
                #region 从交易表抽取数据到低电和拆卸
                try
                {
                    trackStart = DateTime.Now; 
                    _impDao.ExtractLbTamp(extractDate, extractEndDate, out msg);
                    TimeSpan ts = DateTime.Now - trackStart;
                    string logMsg = "抽取拆卸和低电,时间段:(" + extractDate.ToShortDateString() + "到" + extractEndDate.ToShortDateString() + ");耗时:" + StringUtils.FormatHMS((long)ts.TotalSeconds) + ";" + msg;
                    logger.Info(logMsg);
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                #endregion
                #region 抽取电子标签和iC卡关联关系
                try
                {
                    trackStart = DateTime.Now;
                    _impDao.ExtractOBUCardRel(extractDate, extractEndDate, out msg);
                    TimeSpan ts = DateTime.Now - trackStart;
                    string logMsg = "抽取签卡关系,时间段:(" + extractDate.ToShortDateString() + "到" + extractEndDate.ToShortDateString() + ");耗时:" + StringUtils.FormatHMS((long)ts.TotalSeconds) + ";" + msg;
                    logger.Info(logMsg);
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                #endregion
                extractDate = extractDate.AddDays(GetAccDays());
                Thread.Sleep(100);
            }
            return rt;
        }
        /// <summary>
        /// 进行数据统计，中间表生成 改为2天一组按日进行统计
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static short StatData(DateTime startTime, DateTime endTime)
        {
            short rt = 0;
            DateTime extractDate = startTime.Date;//2014-04-12 00:00:00
            while (extractDate < endTime.Date)
            {
                DateTime extractEndDate = extractDate.AddDays(GetAccDays());//2014-04-13 00:00:00
                if (extractEndDate > endTime.Date)
                {
                    extractEndDate = endTime.Date;
                }
                DateTime trackStart = DateTime.Now;string msg = string.Empty;
                #region 重新生成当天的交易中间表
                //统计生成标签交易中间表
                try
                {
                    trackStart = DateTime.Now;
                    _impDao.StatOBUByDay(extractDate, extractEndDate, out msg);
                    TimeSpan ts = DateTime.Now - trackStart;
                    logger.Info("统计标签交易,时间段:(" + extractDate.ToShortDateString() + "到" + extractEndDate.ToShortDateString() + ")耗时:" + StringUtils.FormatHMS((long)ts.TotalSeconds));
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                //统计生成天线交易中间表
                try
                {
                    trackStart = DateTime.Now;
                    _impDao.StatAIRByDay(extractDate, extractEndDate, out msg);
                    TimeSpan ts = DateTime.Now - trackStart;
                    logger.Info("统计天线交易,时间段:(" + extractDate.ToShortDateString() + "到" + extractEndDate.ToShortDateString() + ")耗时:" + StringUtils.FormatHMS((long)ts.TotalSeconds));
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                //统计生成IC卡交易中间表
                try
                {
                    trackStart = DateTime.Now;
                    _impDao.StatCARDByDay(extractDate, extractEndDate, out msg);
                    TimeSpan ts = DateTime.Now - trackStart;
                    logger.Info("统计IC卡交易,时间段:(" + extractDate.ToShortDateString() + "到" + extractEndDate.ToShortDateString() + ")耗时:" + StringUtils.FormatHMS((long)ts.TotalSeconds));
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                #endregion
                extractDate = extractDate.AddDays(GetAccDays());//
            }
            return rt;
        }
        public static short AnalyUnreadData(DateTime startTime, DateTime endTime)
        {
            short rt = 0;
            DateTime extractDate = startTime.Date;//2014-04-12 00:00:00
            while (extractDate < endTime.Date)
            {
                DateTime extractEndDate = extractDate.AddDays(GetAccDays());//2014-04-13 00:00:00
                if (extractEndDate > endTime.Date)
                {
                    extractEndDate = endTime.Date;
                }
                DateTime trackStart = DateTime.Now; string msg = string.Empty;
                #region 漏读分析
                try
                {
                    trackStart = DateTime.Now;
                    UNReadTransService.Extract(extractDate, extractEndDate, out msg);
                    TimeSpan ts = DateTime.Now - trackStart;
                    string logMsg = "漏读分析,时间段:(" + extractDate.ToShortDateString() + "到" + extractEndDate.ToShortDateString() + ");耗时:" + StringUtils.FormatHMS((long)ts.TotalSeconds) + ";" + msg;
                    logger.Info(logMsg);
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                    logger.Error(ex);
                }
                #endregion
                #region 统计生成漏读交易中间表
                try
                {
                    trackStart = DateTime.Now;
                    UNReadTransService.StatTransCount(extractDate, extractEndDate);
                    TimeSpan ts = DateTime.Now - trackStart;
                    logger.Info("统计漏读,时间段:(" + extractDate.ToShortDateString() + "到" + extractEndDate.ToShortDateString() + ")耗时:" + StringUtils.FormatHMS((long)ts.TotalSeconds));
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                #endregion
                extractDate = extractDate.AddDays(GetAccDays());//
            }
            return rt;
        }
        /// <summary>
        /// 签卡关系抽取
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="msg"></param>
        public static void ExtractOBUCardRel(DateTime startTime, DateTime endTime, out string msg)
        {
            _impDao.ExtractOBUCardRel(startTime, endTime, out msg);
        }
        /// <summary>
        /// 装载原始数据
        /// 装载表字段
        ///读取行数         = 76458
        ///跳过行数         = 0
        ///装入行数         = 76458
        ///拒绝行数         = 0
        ///删除行数         = 0
        ///落实行数         = 76458
        ///汇总表字段 四张表顺序 入口成功汇总,入口失败汇总,出口成功汇总,出口失败汇总
        ///表名,汇总天数,汇总条数
        /// </summary>
        public static DataSet LoadRawData(DateTime startTime, DateTime endTime, string entry_zh, string entry_zh_msg,
            string entry_special, string entry_special_msg, string exit_zh, string exit_zh_msg,
            string exit_special, string exit_special_msg)
        {
            return _impDao.LoadRawData(startTime, endTime, entry_zh, entry_zh_msg, entry_special, entry_special_msg
                , exit_zh, exit_zh_msg, exit_special, exit_special_msg);
        }
        public static DataSet StatRawData(DateTime startTime, DateTime endTime)
        {
            return _impDao.StatRawData(startTime, endTime);
        }
        #region 最新版智能统计
        /// <summary>
        /// 最新版标签统计方法v2.1
        /// </summary>
        /// <param name="statIndic">
        /// 统计指标
        /// </param>
        /// <param name="statGroup">
        /// 统计分组条件
        /// 年、月、周、日、小时 分别用STYear,STMonth,STWeek,STDay,STHour表示
        /// SUPPLIER_NAME[厂商],SPEC_NAME[型号],CONTRACT_NO[合同编号],ROAD_NAME[路段],PLAZA_NAME[广场]
        /// </param>
        /// <param name="startTime">统计开始时间</param>
        /// <param name="endTime">统计结束时间</param>
        /// <param name="statCondition">
        /// 统计除时间外的其他条件 
        /// </param>
        /// <param name="fromTempTable">从统计标签临时表中进行统计</param>
        /// <returns></returns>
        public static DataTable GetOBUStatByQ(DateTime startTime, DateTime endTime, string statIndic, int statTType, string spin, string specin, string cnin,
            string rdin, string plazain, string lanein, bool statUnread, bool fromTemp, string[] numstartArray, string[] numendArray)
        {
            string onepassCode = CodeConfigService.GetOnePassMode();
            string devExCode = CodeConfigService.GetOBUDevExModes();
            string lexCode = CodeConfigService.GetOBULExModes();
			string obuNocardCode = CodeConfigService.GetOBUNOCardMode ();
            if (statIndic.Trim() == string.Empty)//默认统计的指标
            {
                statIndic = "TRANSCOUNT,TRANSDCOUNT,ONEPASS_TRANSCOUNT,ONEPASS_RATE,DEVSUC_TRANSCOUNT,DEVSUC_RATE,DEVEX_TRANSCOUNT,DEVEX_RATE,LEX_TRANSCOUNT,LEX_RATE"
                    + ",TAMP_TRANSCOUNT,TAMP_RATE,LB_TRANSCOUNT,LB_RATE";
                if (statUnread)
                {
                    statIndic = statIndic + ",UNREAD_TRANSCOUNT,UNREAD_RATE";
                }
            }
            #region 根据列启动任务
            List<Task> allTask = new List<Task>();
            //开启多个任务;
            Task<DataTable> statTask0=null;
            if (statIndic.Contains("TAMP_RATE") || statIndic.Contains("LB_RATE"))
            {
                statTask0  = new Task<DataTable>(() =>_impDao.GetOBUDsStatByQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray));
                statTask0.Start(); allTask.Add(statTask0);//交易个数
            }
            Task<DataTable> statTask1 = new Task<DataTable>(()=>GetOBUTrStatByModeQ(startTime,endTime,statTType,spin,specin,cnin,rdin,plazain,lanein,fromTemp,numstartArray,numendArray));
            statTask1.Start(); allTask.Add(statTask1);
            DataTable[] StatTable = new DataTable[2];//0统计交易个数,1统计icard分组的交易次数
            DataTable tampTable = null; Task<DataTable> tampTask = null;
            if (statIndic.Contains("TAMP_TRANSCOUNT") || statIndic.Contains("TAMP_RATE"))//拆卸交易率指标
            {
                tampTask = new Task<DataTable>(() => TransTampService.GetOBUStatByQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray));
                tampTask.Start(); allTask.Add(tampTask);
            }
            DataTable lbTable = null; Task<DataTable> lbTask = null;
            if (statIndic.Contains("LB_TRANSCOUNT") || statIndic.Contains("LB_RATE"))//低电交易率指标
            {
                lbTask = new Task<DataTable>(() => TransLBService.GetOBUStatByQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray));
                lbTask.Start(); allTask.Add(lbTask);
            }
            DataTable unreadTable = null; Task<DataTable> unreadTask = null;
            if (statIndic.Contains("UNREAD_TRANSCOUNT") || statIndic.Contains("UNREAD_RATE"))//漏读交易指标
            {
                unreadTask = new Task<DataTable>(() => UNReadTransService.GetOBUStatByQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray));
                unreadTask.Start(); allTask.Add(unreadTask);
            }
            //结束任务
            //等待任务线程完成
            Task.WaitAll(allTask.ToArray());
            StatTable[1] = statTask1.Result;
            if (statTask0 != null) { StatTable[0] = statTask0.Result; }
            if (tampTask != null) { tampTable = tampTask.Result; }
            if (lbTask != null) { lbTable = lbTask.Result; }
            if (unreadTask != null) { unreadTable = unreadTask.Result; }
            #endregion
            //组合结果集table
            DataView dv = new DataView(StatTable[1]);
            List<string> columnNames = new List<string>();
            for (int i = 0; i < StatTable[1].Columns.Count - 2; i++)
            {
                columnNames.Add(StatTable[1].Columns[i].ColumnName);
            }
            DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
            #region 加入指标列信息
            resultTable.Columns.Add("TRANSCOUNT", typeof(Int32));
            if (statIndic.Contains("TAMP_RATE") || statIndic.Contains("LB_RATE"))
            {
                resultTable.Columns.Add("TRANSDCOUNT", typeof(Int32));
            }
            if (statIndic.Contains("TAMP_TRANSCOUNT") || statIndic.Contains("TAMP_RATE"))//拆卸交易率指标
            {
                resultTable.Columns.Add("TAMP_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("TAMP_RATE", typeof(decimal), "iif(TRANSDCOUNT>0,TAMP_TRANSCOUNT/TRANSDCOUNT,null)");
            }
            if (statIndic.Contains("LB_TRANSCOUNT") || statIndic.Contains("LB_RATE"))//低电交易率指标
            {
                resultTable.Columns.Add("LB_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("LB_RATE", typeof(decimal), "iif(TRANSDCOUNT>0,LB_TRANSCOUNT/TRANSDCOUNT,null)");
            }
            if (statIndic.Contains("UNREAD_TRANSCOUNT") || statIndic.Contains("UNREAD_RATE"))//漏读交易指标
            {
                resultTable.Columns.Add("UNREAD_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("UNREAD_RATE", typeof(decimal), "iif(TRANSCOUNT>0,UNREAD_TRANSCOUNT/TRANSCOUNT,null)");
            }
            DataTable onePassTable = null;
            if (statIndic.Contains("ONEPASS_TRANSCOUNT") || statIndic.Contains("ONEPASS_RATE"))//一次通过率指标
            {
                onePassTable = new DataView(StatTable[1], "ICARD_WORKMODE IN (" + onepassCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
                resultTable.Columns.Add("ONEPASS_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("ONEPASS_RATE", typeof(decimal), "iif(TRANSCOUNT>0,ONEPASS_TRANSCOUNT/TRANSCOUNT,null)");
            }
			DataTable obuNocardTable = null;
			if(statIndic.Contains("DEVEX_RATE")|| statIndic.Contains("LEX_RATE")){
				obuNocardTable = new DataView(StatTable[1],"ICARD_WORKMODE IN (" + obuNocardCode + ")",string.Empty,DataViewRowState.CurrentRows).ToTable();
			}
			DataTable devExTable = null;
			if (statIndic.Contains("DEVEX_TRANSCOUNT") || statIndic.Contains("DEVEX_RATE"))//设备异常率指标
			{
				devExTable = new DataView(StatTable[1], "ICARD_WORKMODE IN (" + devExCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
				resultTable.Columns.Add("DEVEX_TRANSCOUNT", typeof(Int32));
				resultTable.Columns.Add("DEVEX_RATE", typeof(decimal), "iif(TRANSCOUNT>0,DEVEX_TRANSCOUNT/TRANSCOUNT,null)");
			}
			DataTable lExTable = null;
			if (statIndic.Contains("LEX_TRANSCOUNT") || statIndic.Contains("LEX_RATE"))//逻辑异常率指标
			{
				lExTable = new DataView(StatTable[1], "ICARD_WORKMODE IN (" + lexCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
				resultTable.Columns.Add("LEX_TRANSCOUNT", typeof(Int32));
				resultTable.Columns.Add("LEX_RATE", typeof(decimal), "iif(TRANSCOUNT>0,LEX_TRANSCOUNT/TRANSCOUNT,null)");
			}
            if (statIndic.Contains("DEVSUC_TRANSCOUNT") || statIndic.Contains("DEVSUC_RATE"))//设备成功率指标
            {
				resultTable.Columns.Add("DEVSUC_TRANSCOUNT", typeof(Int32),"ONEPASS_TRANSCOUNT+LEX_TRANSCOUNT");
                resultTable.Columns.Add("DEVSUC_RATE", typeof(decimal), "iif(TRANSCOUNT>0,DEVSUC_TRANSCOUNT/TRANSCOUNT,null)");
            }
            #endregion
            #region 计算赋值
            StringBuilder selText = new StringBuilder();
            for (int i = 0; i < resultTable.Rows.Count; i++)
            {
                selText.Clear(); DataRow curRow = resultTable.Rows[i];
                for (int j = 0; j < columnNames.Count; j++)
                {
                    string clName = columnNames[j];
                    DataColumn _dtc = resultTable.Columns[clName];
                    if (j > 0)
                    {
                        selText.Append(" AND ");
                    }
                    if (_dtc.DataType.Equals(typeof(string)))
                    {
                        selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                    }
                    else
                    {
                        selText.Append(clName + "=" + curRow[clName].ToString());
                    }
                }
                object unread_sum_cnt = 0;
                int ii_temp = 0;
                object sum_cnt = StatTable[1].Compute("sum(TRANSCOUNT)", selText.ToString());
                if (!int.TryParse(sum_cnt.ToString(), out ii_temp))
                {
                    sum_cnt = 0;
                }
                if (unreadTable != null)
                {
                    unread_sum_cnt = unreadTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                    if (!int.TryParse(unread_sum_cnt.ToString(), out ii_temp))
                    {
                        unread_sum_cnt = 0;
                    }
                }
                if (statUnread)
                {
                    if (unread_sum_cnt != DBNull.Value)
                    {
                        sum_cnt = (sum_cnt == DBNull.Value ? 0 : Convert.ToInt32(sum_cnt)) + Convert.ToInt32(unread_sum_cnt);
                    }
                }
                curRow["TRANSCOUNT"] = sum_cnt;
                if (resultTable.Columns.Contains("TRANSDCOUNT"))
                {
                    curRow["TRANSDCOUNT"] = StatTable[0].Compute("sum(TRANSDCOUNT)", selText.ToString());
                }
                if (resultTable.Columns.Contains("ONEPASS_TRANSCOUNT"))
                {
                    curRow["ONEPASS_TRANSCOUNT"] = onePassTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                object obuNoCardCnt = 0;
                if (obuNocardTable != null)
                {
                    obuNoCardCnt = obuNocardTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                    if (obuNoCardCnt == null || obuNoCardCnt == DBNull.Value)
                    {
                        obuNoCardCnt = 0;
                    }
                }
                if (resultTable.Columns.Contains("DEVEX_TRANSCOUNT"))//设备异常加obu无卡*0.5
                {
					object devex_cnt = devExTable.Compute("sum(TRANSCOUNT)", selText.ToString());
					if(devex_cnt==DBNull.Value) { devex_cnt = 0; }
					curRow["DEVEX_TRANSCOUNT"] = Convert.ToInt32(devex_cnt)
						+ Convert.ToInt32(Convert.ToDouble(obuNoCardCnt)*0.5);
                }
                if (resultTable.Columns.Contains("LEX_TRANSCOUNT"))//逻辑异常加obu无卡*0.5
                {
					object lex_cnt = lExTable.Compute("sum(TRANSCOUNT)", selText.ToString());
					if(lex_cnt == DBNull.Value){ lex_cnt = 0; }
					curRow["LEX_TRANSCOUNT"] = Convert.ToInt32(lex_cnt)
						+ Convert.ToInt32(Convert.ToDouble(obuNoCardCnt)*0.5);
                }
                if (resultTable.Columns.Contains("TAMP_TRANSCOUNT"))
                {
                    curRow["TAMP_TRANSCOUNT"] = tampTable.Compute("sum(TRANSDCOUNT)", selText.ToString());
                }
                if (resultTable.Columns.Contains("LB_TRANSCOUNT"))
                {
                    curRow["LB_TRANSCOUNT"] = lbTable.Compute("sum(TRANSDCOUNT)", selText.ToString());
                }
                if (resultTable.Columns.Contains("UNREAD_TRANSCOUNT"))
                {
                    curRow["UNREAD_TRANSCOUNT"] = unreadTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
            }
            #endregion
            return resultTable;
        }
        /// <summary>
        /// 单独针对交易次数进行处理
        /// 包含workmode分组
        /// </summary>
        public static DataTable GetOBUTrStatByModeQ(DateTime startTime, DateTime endTime,  int statTType, string spin, string specin, string cnin,
            string rdin, string plazain, string lanein, bool fromTemp, string[] numstartArray, string[] numendArray)
        {
            TimeSpan ts = endTime - startTime;
            //如果从标签临时表或者标签号段统计数据或者只是统计小于2天的数据或者进行按时统计时则不使用中间表
            //无法使用中间表的4种情况1.使用了标签临时表,2.使用了标签号段,3.统计时间小于2天,4.进行按小时统计
            if (fromTemp || (numstartArray != null && numstartArray.Length > 0)||ts.TotalDays<=2||statTType==4)
            {
                return _impDao.GetOBUTrStatByQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray);
            }
            else
            {
                //数据库里的时间是精确到秒的，因此 减1s就可以处理从trans_note中查询和从中间表中查询凌晨时重合的问题
                //如2014-5-1 12:00:00 到2014-5-3 12:00:00 则拆分成三段
                //1. 2014-5-1 12:00:00至2014-5-1 23:59:59 
                //2. 2014-5-2 00:00:00至2014-5-2 23:59:59
                //3. 2014-5-3 00:00:00至2014-5-3 12:00:00
                Task<DataTable> seg1Task = null;
                List<Task> allTask = new List<Task>();
                DateTime mStartTime = startTime; DateTime mEndTime = endTime;
                if (startTime.Hour > 0 || startTime.Minute > 0)//不是整天
                {
                    DateTime seg1endTime = startTime.Date.AddDays(1); mStartTime = seg1endTime;
                    seg1Task = new Task<DataTable>(() => _impDao.GetOBUTrStatByQ(startTime, seg1endTime.AddSeconds(-1), statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray));
                    seg1Task.Start();
                    allTask.Add(seg1Task);
                }
                Task<DataTable> seg3Task = null;
                if (endTime.Hour < 23 || endTime.Minute < 59)//不是整天
                {
                    DateTime seg3startTime = endTime.Date; mEndTime = seg3startTime.AddSeconds(-1);//endtime的日期部分减1s即为
                    seg3Task = new Task<DataTable>(() => _impDao.GetOBUTrStatByQ(seg3startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray));
                    seg3Task.Start();
                    allTask.Add(seg3Task);
                }
                Task<DataTable> seg2Task = new Task<DataTable>(() => OBUTransByDayService.GetOBUStatByMModeQ(mStartTime, mEndTime, statTType, spin, specin, cnin, rdin, plazain, lanein));
                seg2Task.Start();
                allTask.Add(seg2Task);
                Task.WaitAll(allTask.ToArray());//等待3段统计结果都完成
                DataTable seg2Table = seg2Task.Result;
                if (seg1Task != null) { 
                    seg2Table.Merge(seg1Task.Result);
                }
                if (seg3Task != null) { 
                    seg2Table.Merge(seg3Task.Result); 
                }
                DataView dv = new DataView(seg2Table);
                List<string> columnNames = new List<string>();
                for (int i = 0; i < seg2Table.Columns.Count - 1; i++)
                {
                    columnNames.Add(seg2Table.Columns[i].ColumnName);
                }
                DataTable resultTable = dv.ToTable(true,columnNames.ToArray());
                resultTable.Columns.Add("TRANSCOUNT",typeof(int));
                StringBuilder selText = new StringBuilder();
                for (int i = 0; i < resultTable.Rows.Count; i++)
                {
                    selText.Clear(); DataRow curRow = resultTable.Rows[i];
                    for (int j = 0; j < columnNames.Count; j++)
                    {
                        string clName = columnNames[j];
                        DataColumn _dtc = resultTable.Columns[clName];
                        if (j > 0)
                        {
                            selText.Append(" AND ");
                        }
                        if (_dtc.DataType.Equals(typeof(string)))
                        {
                            selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                        }
                        else
                        {
                            selText.Append(clName + "=" + curRow[clName].ToString());
                        }
                    }
                    curRow["TRANSCOUNT"] = seg2Table.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                return resultTable;
            }
        }
        /// <summary>
        /// 天线综合查询
        /// </summary>
        public static DataTable GetAIRStatByQ(DateTime startTime, DateTime endTime, string statIndic, int statTType,string spin,string specin
            ,string rdin,string plazain,string lanein,bool statUnread)
        {
            string onepassCode = CodeConfigService.GetOnePassMode();
            string devExCode = CodeConfigService.GetAIRDevExModes();
            string lexCode = CodeConfigService.GetAIRLExModes();
            List<Task> allTask = new List<Task>();
            if (statIndic.Trim() == string.Empty)//默认统计的指标
            {
                statIndic = "TRANSCOUNT,ONEPASS_TRANSCOUNT,ONEPASS_RATE,DEVSUC_TRANSCOUNT,DEVSUC_RATE,DEVEX_TRANSCOUNT,DEVEX_RATE,LEX_TRANSCOUNT,LEX_RATE";
                if (statUnread)
                {
                    statIndic = statIndic + ",UNREAD_TRANSCOUNT,UNREAD_RATE";
                }
            }
            //开启多个任务
            DataTable StatTable = null;
            Task<DataTable> statTask = new Task<DataTable>(()=>GetAIRStatByModeQ(startTime, endTime, statTType, spin, specin, rdin, plazain, lanein));
            statTask.Start();allTask.Add(statTask);
            Task<DataTable> unTask =null; DataTable unreadTable = null;
            if (statIndic.Contains("UNREAD_TRANSCOUNT") || statIndic.Contains("UNREAD_RATE"))//漏读交易指标
            {
                unTask = new Task<DataTable>(() => UNReadTransService.GetAIRStatByQ(startTime, endTime, statTType, spin, specin, rdin, plazain, lanein));
                unTask.Start();allTask.Add(unTask);
            }
            Task.WaitAll(allTask.ToArray());
            StatTable = statTask.Result;
            if (unTask != null) { unreadTable = unTask.Result; }
            //组合结果集table
            DataView dv = new DataView(StatTable);
            List<string> columnNames = new List<string>();
            for (int i = 0; i < StatTable.Columns.Count - 2; i++)
            {
                columnNames.Add(StatTable.Columns[i].ColumnName);
            }
            DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
            #region 加入指标列信息
            resultTable.Columns.Add("TRANSCOUNT", typeof(Int32)); resultTable.Columns.Add("TRANSDCOUNT", typeof(Int32));
            DataTable onePassTable = null;
            if (statIndic.Contains("ONEPASS_TRANSCOUNT") || statIndic.Contains("ONEPASS_RATE"))//一次通过率指标
            {
                onePassTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + onepassCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
                resultTable.Columns.Add("ONEPASS_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("ONEPASS_RATE", typeof(decimal), "iif(TRANSCOUNT>0,ONEPASS_TRANSCOUNT/TRANSCOUNT,null)");
            }
            DataTable devExTable = null;
            if (statIndic.Contains("DEVEX_TRANSCOUNT") || statIndic.Contains("DEVEX_RATE"))//设备异常率指标
            {
                devExTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + devExCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
                resultTable.Columns.Add("DEVEX_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("DEVEX_RATE", typeof(decimal), "iif(TRANSCOUNT>0,DEVEX_TRANSCOUNT/TRANSCOUNT,null)");
            }
            DataTable lExTable = null;
            if (statIndic.Contains("LEX_TRANSCOUNT") || statIndic.Contains("LEX_RATE"))//逻辑异常率指标
            {
                lExTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + lexCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
                resultTable.Columns.Add("LEX_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("LEX_RATE", typeof(decimal), "iif(TRANSCOUNT>0,LEX_TRANSCOUNT/TRANSCOUNT,null)");
            }
			if (statIndic.Contains("DEVSUC_TRANSCOUNT") || statIndic.Contains("DEVSUC_RATE"))//设备成功率指标
			{
				resultTable.Columns.Add("DEVSUC_TRANSCOUNT", typeof(Int32),"ONEPASS_TRANSCOUNT+LEX_TRANSCOUNT");
				resultTable.Columns.Add("DEVSUC_RATE", typeof(decimal), "iif(TRANSCOUNT>0,DEVSUC_TRANSCOUNT/TRANSCOUNT,null)");
			}
            if (statIndic.Contains("UNREAD_TRANSCOUNT") || statIndic.Contains("UNREAD_RATE"))//漏读交易指标
            {
                resultTable.Columns.Add("UNREAD_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("UNREAD_RATE", typeof(decimal), "iif(TRANSCOUNT>0,UNREAD_TRANSCOUNT/TRANSCOUNT,null)");
            }
            #endregion
            #region 计算赋值
            StringBuilder selText = new StringBuilder();
            for (int i = 0; i < resultTable.Rows.Count; i++)
            {
                selText.Clear(); DataRow curRow = resultTable.Rows[i];
                for (int j = 0; j < columnNames.Count; j++)
                {
                    string clName = columnNames[j];
                    DataColumn _dtc = resultTable.Columns[clName];
                    if (j > 0)
                    {
                        selText.Append(" AND ");
                    }
                    if (_dtc.DataType.Equals(typeof(string)))
                    {
                        selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                    }
                    else
                    {
                        selText.Append(clName + "=" + curRow[clName].ToString());
                    }
                }
                object unread_sum_cnt = 0;
                int ii_temp = 0;
                object sum_cnt = StatTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                if (!int.TryParse(sum_cnt.ToString(), out ii_temp))
                {
                    sum_cnt = 0;
                }
                if (unreadTable != null)
                {
                    unread_sum_cnt = unreadTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                    if (!int.TryParse(unread_sum_cnt.ToString(), out ii_temp))
                    {
                        unread_sum_cnt = 0;
                    }
                }
                if (statUnread)
                {
                    if (unread_sum_cnt != DBNull.Value)
                    {
                        sum_cnt = (sum_cnt == DBNull.Value ? 0 : Convert.ToInt32(sum_cnt)) + Convert.ToInt32(unread_sum_cnt);
                    }
                }
                curRow["TRANSCOUNT"] = sum_cnt;
                if (resultTable.Columns.Contains("ONEPASS_TRANSCOUNT"))
                {
                    curRow["ONEPASS_TRANSCOUNT"] = onePassTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                if (resultTable.Columns.Contains("DEVEX_TRANSCOUNT"))
                {
                    curRow["DEVEX_TRANSCOUNT"] = devExTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                if (resultTable.Columns.Contains("LEX_TRANSCOUNT"))
                {
                    curRow["LEX_TRANSCOUNT"] = lExTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                if (resultTable.Columns.Contains("UNREAD_TRANSCOUNT"))
                {
                    curRow["UNREAD_TRANSCOUNT"] = unreadTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
            }
            #endregion
            return resultTable;
        }
        /// <summary>
        /// 天线交易次数单独处理
        /// 包含workmode分组
        /// </summary>
        public static DataTable GetAIRStatByModeQ(DateTime startTime, DateTime endTime, int statTType, string spin, string specin
            , string rdin, string plazain, string lanein)
        {
            TimeSpan ts = endTime - startTime;
            //如果间隔少于两天，或者需要按时查询汇总的话，无法使用中间表
            //否则将时间分成3段
            if (statTType == 4||ts.TotalDays<2)
            {
                return _impDao.GetAIRStatByQ(startTime, endTime, statTType, spin, specin, rdin, plazain, lanein);
            }
            else
            {
                Task<DataTable> seg1Task = null;
                List<Task> allTask = new List<Task>();
                DateTime mStartTime = startTime; DateTime mEndTime = endTime;
                if (startTime.Hour > 0 || startTime.Minute > 0)//不是整天
                {
                    DateTime seg1endTime = startTime.Date.AddDays(1); mStartTime = seg1endTime;
                    seg1Task = new Task<DataTable>(() => _impDao.GetAIRStatByQ(startTime, seg1endTime.AddSeconds(-1), statTType, spin, specin, rdin, plazain, lanein));
                    seg1Task.Start();
                    allTask.Add(seg1Task);
                }
                Task<DataTable> seg3Task = null;
                if (endTime.Hour < 23 || endTime.Minute < 59)//不是整天
                {
                    DateTime seg3startTime = endTime.Date; mEndTime = seg3startTime.AddSeconds(-1);//endtime的日期部分减1s即为
                    seg3Task = new Task<DataTable>(() =>_impDao.GetAIRStatByQ(seg3startTime, endTime, statTType, spin, specin, rdin, plazain, lanein));
                    seg3Task.Start();
                    allTask.Add(seg3Task);
                }
                Task<DataTable> seg2Task = new Task<DataTable>(()=>AIRWIRETransByDayService.GetAIRStatByMModeQ(startTime,endTime,statTType,spin,specin,rdin,plazain,lanein));
                seg2Task.Start();
                allTask.Add(seg2Task);
                Task.WaitAll(allTask.ToArray());//等待3段统计结果都完成
                DataTable seg2Table = seg2Task.Result;
                if (seg1Task != null)
                {
                    seg2Table.Merge(seg1Task.Result);
                }
                if (seg3Task != null)
                {
                    seg2Table.Merge(seg3Task.Result);
                }
                DataView dv = new DataView(seg2Table);
                List<string> columnNames = new List<string>();
                for (int i = 0; i < seg2Table.Columns.Count - 1; i++)
                {
                    columnNames.Add(seg2Table.Columns[i].ColumnName);
                }
                DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
                resultTable.Columns.Add("TRANSCOUNT", typeof(int));
                StringBuilder selText = new StringBuilder();
                for (int i = 0; i < resultTable.Rows.Count; i++)
                {
                    selText.Clear(); DataRow curRow = resultTable.Rows[i];
                    for (int j = 0; j < columnNames.Count; j++)
                    {
                        string clName = columnNames[j];
                        DataColumn _dtc = resultTable.Columns[clName];
                        if (j > 0)
                        {
                            selText.Append(" AND ");
                        }
                        if (_dtc.DataType.Equals(typeof(string)))
                        {
                            selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                        }
                        else
                        {
                            selText.Append(clName + "=" + curRow[clName].ToString());
                        }
                    }
                    curRow["TRANSCOUNT"] = seg2Table.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                return resultTable;
            }
        }
        /// <summary>
        /// 卡查询统计
        /// </summary>
        public static DataTable GetCARDStatByQ(DateTime startTime, DateTime endTime, string statIndic, int statTType, string spin, string specin, string cnin,
            string rdin, string plazain, string lanein, bool fromTemp, string[] numstartArray, string[] numendArray)
        {
            string onepassCode = CodeConfigService.GetOnePassMode();
            string devExCode = CodeConfigService.GetCARDDevExModes();
            string lexCode = CodeConfigService.GetCARDLExModes();
            if (statIndic.Trim() == string.Empty)//默认统计的指标
            {
                statIndic = "TRANSCOUNT,TRANSDCOUNT,ONEPASS_TRANSCOUNT,ONEPASS_RATE,DEVSUC_TRANSCOUNT,DEVSUC_RATE,DEVEX_TRANSCOUNT,DEVEX_RATE,LEX_TRANSCOUNT,LEX_RATE";
            }
            DataTable StatTable = GetCARDStatByModeQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray);
            //组合结果集table
            DataView dv = new DataView(StatTable);
            List<string> columnNames = new List<string>();
            for (int i = 0; i < StatTable.Columns.Count - 2; i++)
            {
                columnNames.Add(StatTable.Columns[i].ColumnName);
            }
            DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
            #region 加入指标列信息
            resultTable.Columns.Add("TRANSCOUNT", typeof(Int32)); 
            DataTable onePassTable = null;
            if (statIndic.Contains("ONEPASS_TRANSCOUNT") || statIndic.Contains("ONEPASS_RATE"))//一次通过率指标
            {
                onePassTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + onepassCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
                resultTable.Columns.Add("ONEPASS_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("ONEPASS_RATE", typeof(decimal), "iif(TRANSCOUNT>0,ONEPASS_TRANSCOUNT/TRANSCOUNT,null)");
            }
			DataTable devExTable = null;
			if (statIndic.Contains("DEVEX_TRANSCOUNT") || statIndic.Contains("DEVEX_RATE"))//设备异常率指标
			{
				devExTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + devExCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
				resultTable.Columns.Add("DEVEX_TRANSCOUNT", typeof(Int32));
				resultTable.Columns.Add("DEVEX_RATE", typeof(decimal), "iif(TRANSCOUNT>0,DEVEX_TRANSCOUNT/TRANSCOUNT,null)");
			}
			DataTable lExTable = null;
			if (statIndic.Contains("LEX_TRANSCOUNT") || statIndic.Contains("LEX_RATE"))//逻辑异常率指标
			{
				lExTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + lexCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
				resultTable.Columns.Add("LEX_TRANSCOUNT", typeof(Int32));
				resultTable.Columns.Add("LEX_RATE", typeof(decimal), "iif(TRANSCOUNT>0,LEX_TRANSCOUNT/TRANSCOUNT,null)");
			}
            if (statIndic.Contains("DEVSUC_TRANSCOUNT") || statIndic.Contains("DEVSUC_RATE"))//设备成功率指标
            {
				resultTable.Columns.Add("DEVSUC_TRANSCOUNT", typeof(Int32),"ONEPASS_TRANSCOUNT+LEX_TRANSCOUNT");
                resultTable.Columns.Add("DEVSUC_RATE", typeof(decimal), "iif(TRANSCOUNT>0,DEVSUC_TRANSCOUNT/TRANSCOUNT,null)");
            }
            #endregion
            #region 计算赋值
            StringBuilder selText = new StringBuilder();
            for (int i = 0; i < resultTable.Rows.Count; i++)
            {
                selText.Clear(); DataRow curRow = resultTable.Rows[i];
                for (int j = 0; j < columnNames.Count; j++)
                {
                    string clName = columnNames[j];
                    DataColumn _dtc = resultTable.Columns[clName];
                    if (j > 0)
                    {
                        selText.Append(" AND ");
                    }
                    if (_dtc.DataType.Equals(typeof(string)))
                    {
                        selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                    }
                    else
                    {
                        selText.Append(clName + "=" + curRow[clName].ToString());
                    }
                }
                int ii_temp = 0;
                object sum_cnt = StatTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                if (!int.TryParse(sum_cnt.ToString(), out ii_temp))
                {
                    sum_cnt = 0;
                }
                curRow["TRANSCOUNT"] = sum_cnt;
                if (resultTable.Columns.Contains("ONEPASS_TRANSCOUNT"))
                {
                    curRow["ONEPASS_TRANSCOUNT"] = onePassTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                if (resultTable.Columns.Contains("DEVEX_TRANSCOUNT"))
                {
                    curRow["DEVEX_TRANSCOUNT"] = devExTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                if (resultTable.Columns.Contains("LEX_TRANSCOUNT"))
                {
                    curRow["LEX_TRANSCOUNT"] = lExTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
            }
            #endregion
            return resultTable;
        }
        /// <summary>
        /// 获得卡统计数据
        /// 包含代码集icard_workmode分组
        /// </summary>
        public static DataTable GetCARDStatByModeQ(DateTime startTime, DateTime endTime, int statTType, string spin, string specin, string cnin,
            string rdin, string plazain, string lanein, bool fromTemp, string[] numstartArray, string[] numendArray)
        {
            TimeSpan ts = endTime - startTime;
            //如果从标签临时表或者标签号段统计数据或者只是统计小于2天的数据或者进行按时统计时则不使用中间表
            //无法使用中间表的4种情况1.使用了标签临时表,2.使用了标签号段,3.统计时间小于2天,4.进行按小时统计
            if (fromTemp || (numstartArray != null && numstartArray.Length > 0) || ts.TotalDays <= 2 || statTType == 4)
            {
                return _impDao.GetCARDStatByQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray);
            }
            else
            {
                //数据库里的时间是精确到秒的，因此 减1s就可以处理从trans_note中查询和从中间表中查询凌晨时重合的问题
                //如2014-5-1 12:00:00 到2014-5-3 12:00:00 则拆分成三段
                //1. 2014-5-1 12:00:00至2014-5-1 23:59:59 
                //2. 2014-5-2 00:00:00至2014-5-2 23:59:59
                //3. 2014-5-3 00:00:00至2014-5-3 12:00:00
                Task<DataTable> seg1Task = null;
                List<Task> allTask = new List<Task>();
                DateTime mStartTime = startTime; DateTime mEndTime = endTime;
                if (startTime.Hour > 0 || startTime.Minute > 0)//不是整天
                {
                    DateTime seg1endTime = startTime.Date.AddDays(1); mStartTime = seg1endTime;
                    seg1Task = new Task<DataTable>(() => _impDao.GetCARDStatByQ(startTime, seg1endTime.AddSeconds(-1), statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray));
                    seg1Task.Start();
                    allTask.Add(seg1Task);
                }
                Task<DataTable> seg3Task = null;
                if (endTime.Hour < 23 || endTime.Minute < 59)//不是整天
                {
                    DateTime seg3startTime = endTime.Date; mEndTime = seg3startTime.AddSeconds(-1);//endtime的日期部分减1s即为
                    seg3Task = new Task<DataTable>(() => _impDao.GetCARDStatByQ(seg3startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray));
                    seg3Task.Start();
                    allTask.Add(seg3Task);
                }
                Task<DataTable> seg2Task = new Task<DataTable>(() => CARDTransByDayService.GetCARDStatByMModeQ(mStartTime, mEndTime, statTType, spin, specin, cnin, rdin, plazain, lanein));
                seg2Task.Start();
                allTask.Add(seg2Task);
                Task.WaitAll(allTask.ToArray());//等待3段统计结果都完成
                DataTable seg2Table = seg2Task.Result;
                if (seg1Task != null)
                {
                    seg2Table.Merge(seg1Task.Result);
                }
                if (seg3Task != null)
                {
                    seg2Table.Merge(seg3Task.Result);
                }
                DataView dv = new DataView(seg2Table);
                List<string> columnNames = new List<string>();
                for (int i = 0; i < seg2Table.Columns.Count - 1; i++)
                {
                    columnNames.Add(seg2Table.Columns[i].ColumnName);
                }
                DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
                resultTable.Columns.Add("TRANSCOUNT", typeof(int));
                StringBuilder selText = new StringBuilder();
                for (int i = 0; i < resultTable.Rows.Count; i++)
                {
                    selText.Clear(); DataRow curRow = resultTable.Rows[i];
                    for (int j = 0; j < columnNames.Count; j++)
                    {
                        string clName = columnNames[j];
                        DataColumn _dtc = resultTable.Columns[clName];
                        if (j > 0)
                        {
                            selText.Append(" AND ");
                        }
                        if (_dtc.DataType.Equals(typeof(string)))
                        {
                            selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                        }
                        else
                        {
                            selText.Append(clName + "=" + curRow[clName].ToString());
                        }
                    }
                    curRow["TRANSCOUNT"] = seg2Table.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
                return resultTable;
            }
        }
        /// <summary>
        /// 获取以obu分组的明细依据obu的列表
        /// </summary>
        public static DataTable GetOBUGDetailByQ(DateTime startTime, DateTime endTime, string obuin, string rdin, string plazain, string lanein)
        {
            return _impDao.GetOBUGDetailByQ(startTime, endTime, obuin, rdin, plazain, lanein);
        }
        /// <summary>
        /// 获取以天线分组的明细
        /// </summary>
        public static DataTable GetAIRGDetailByQ(DateTime startTime, DateTime endTime,string plazain,string lanein)
        {
            return _impDao.GetAIRGDetailByQ(startTime, endTime, plazain, lanein);
        }
        #endregion
		#region 军车统计
		/// <summary>
		/// 获得军车统计数据
		/// </summary>
		/// <param name="startTime">开始时间</param>
		/// <param name="endTime">结束时间</param>
		public static DataTable GetJZTB(DateTime startTime,DateTime endTime){
			DataTable resultTable = new DataTable ();
			resultTable.Columns.Add ("TB_DATE",typeof(string));//日期
			resultTable.Columns.Add ("SUCC_COUNT", typeof(Int32));//一次通过量
			resultTable.Columns.Add ("GRAY_COUNT", typeof(Int32));//逻辑异常量
			resultTable.Columns.Add ("SPECIAL_COUNT", typeof(Int32));//异常通过量
			resultTable.Columns.Add ("NWU_COUNT", typeof(Int32));//漏读量
			resultTable.Columns.Add ("TRANS_COUNT", typeof(Int32));//总交易量
			resultTable.Columns.Add ("ONEPASS_RATE", typeof(double),"SUCC_COUNT/TRANS_COUNT");//一次通过率
			resultTable.Columns.Add ("DEVSUC_RATE", typeof(double),"(SUCC_COUNT + GRAY_COUNT)/TRANS_COUNT");//交易成功率,一次通过量+逻辑异常量/交易次数
			resultTable.Columns.Add ("LEX_RATE", typeof(double),"GRAY_COUNT/TRANS_COUNT");//逻辑异常量
			List<Task> allTask = new List<Task>();
			Task<DataTable> statTask = new Task<DataTable> (() => _impDao.GetJZTBG (startTime, endTime));
			Task<DataTable> unTask = new Task<DataTable> (() => UNReadTransService.GetJZTBG (startTime, endTime));
			statTask.Start (); unTask.Start ();
			allTask.Add (statTask);  allTask.Add (unTask);
			Task.WaitAll (allTask.ToArray ());
			DataTable statTable = statTask.Result; DataTable unTable = unTask.Result;
			if (statTable.Rows.Count > 0) {
				DataRow rrow = resultTable.NewRow ();
				rrow ["TB_DATE"] = startTime.ToString ("yyyyMMdd") + "-" + endTime.ToString ("MMdd");
				rrow ["TRANS_COUNT"] = statTable.Compute ("SUM(TRANS_COUNT)","1=1");//总交易量
				rrow ["SUCC_COUNT"] = statTable.Compute("SUM(TRANS_COUNT)","ICARD_WORKMODE IN ("+CodeConfigService.GetOnePassMode()+") AND (TABLETYPE = 1 OR TABLETYPE = 2)");//一次通过量
				rrow ["GRAY_COUNT"] = statTable.Compute("SUM(TRANS_COUNT)","ICARD_WORKMODE IN ("+CodeConfigService.GetOBULExModes()+") AND (TABLETYPE = 3 OR TABLETYPE = 4)");//逻辑异常量
				rrow ["SPECIAL_COUNT"] = statTable.Compute("SUM(TRANS_COUNT)","TABLETYPE = 3 OR TABLETYPE = 4");//异常通过量
				rrow ["NWU_COUNT"] = unTable.Compute ("SUM(TRANS_COUNT)","TABLETYPE = 3 OR TABLETYPE = 4");//漏读量
				resultTable.Rows.Add (rrow);
			}
			//
			return resultTable;
		}
		#endregion
		/// <summary>
		/// 获得4张原始表的最后交易时间
		/// </summary>
		public static DateTime[] GetRawMaxTime (){
			return _impDao.GetRawMaxTime ();
		}
		public static DateTime GetRawMinTime(){
			return _impDao.GetRawMinTime ();
		}
    }
}
